using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;
using System.IO;
using DataItems;
using System.Xml;

namespace JeremiXnaGame
{
    public class JeremiGame : Microsoft.Xna.Framework.Game
    {
        private static JeremiGame singleton;
        public static JeremiGame Singleton { get { return singleton; } }

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Settings settings;

        public int screenWidth, screenHeight;
        ScreenManager screenManager;

        public JeremiGame()
        {
            singleton = this;

            settings = Settings.Load();
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.IsFullScreen = settings.Fullscreen;
            graphics.PreferredBackBufferWidth = settings.ScreenWidth;
            graphics.PreferredBackBufferHeight = settings.ScreenHeight;
            graphics.SynchronizeWithVerticalRetrace = settings.VSync;
            graphics.MinimumPixelShaderProfile = ShaderProfile.PS_2_0;
            graphics.MinimumVertexShaderProfile = ShaderProfile.VS_2_0;

            IsMouseVisible = settings.IsMouseVisible;
            IsFixedTimeStep = settings.IsFixedTimeStep;

            screenManager = new ScreenManager(this);
            Components.Add(screenManager);

            if (settings.SkipMenu)
            {
                LoadingScreen.Load(screenManager, true,PlayerIndex.One, new GameplayScreen());
            }
            else
            {
                screenManager.AddScreen(new BackgroundScreen(), null);
                screenManager.AddScreen(new MainMenuScreen(), null);
            }

        }

        protected override void Initialize()
        {
            graphics.DeviceReset += new EventHandler(OnDeviceReset);
            OnDeviceReset(null, EventArgs.Empty);

            GetMap();

            base.Initialize();
        }

        private void GetMap()
        {

            XmlTextReader reader = new XmlTextReader(@"C:\Users\jeremi\Desktop\Tiled\Jeremi Maps\Map1.tmx.xml");
            Map map = new Map();

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        if (reader.Name == "map")
                        {
                            map.Width = int.Parse(reader.GetAttribute(2));
                            map.Height = int.Parse(reader.GetAttribute(3));
                            map.TileWidth = int.Parse(reader.GetAttribute(4));
                            map.TileHeight = int.Parse(reader.GetAttribute(5));
                        }

                        if (reader.Name == "properties")
                        {
                            reader.Read(); reader.Read();
                            map.Name = reader.GetAttribute(1);
                        }

                        if (reader.Name == "tileset")
                        {
                            
                        }


                        break;

                    case XmlNodeType.Text: //Display the text in each element.
                        Console.WriteLine(reader.Value);
                        break;

                    case XmlNodeType.EndElement: //Display the end of the element.
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                }
            }
        }


        private void OnDeviceReset(object sender, EventArgs e)
        {
            Helper.Msg("Device Reset <" + screenWidth + ", " + screenHeight + ">");
            
            screenWidth = GraphicsDevice.Viewport.Width;
            screenHeight = GraphicsDevice.Viewport.Height;

            GraphicsDevice.RenderState.DepthBufferEnable = true;
            GraphicsDevice.RenderState.DepthBufferWriteEnable = true;

            GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
            GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;

            GraphicsDevice.RenderState.ReferenceAlpha = 128;
            GraphicsDevice.RenderState.AlphaFunction = CompareFunction.Greater;
            
            GraphicsDevice.RenderState.AlphaSourceBlend = Blend.SourceAlpha;
            GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.InverseSourceAlpha;

        }


        public void ChangeResolution(int width, int height)
        {
            graphics.PreferredBackBufferHeight = height;
            graphics.PreferredBackBufferWidth = width;
            graphics.ApplyChanges();
        }



        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);


        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.F))
                Helper.TakeScreenShot(GraphicsDevice);
            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);



            base.Draw(gameTime);
        }
    }
}
